﻿using System;
using XRM.Security.DataAccess.Context;
using XRM.Security.DataAccess.Repositories.Impl;

namespace XRM.Security.DataAccess
{
    public class DataManager : IDataManager
    {
        public IDbContext ContextDb
        {
            get; private set;
        }

        public DataManager()
        {
            ContextDb = new MyContext();            
        }

        #region Repositories

        private UserRegisterRepository _userRegisterRepository;
        public UserRegisterRepository UserRegisterRepository 
        { 
            get
            {
                return _userRegisterRepository ?? ( _userRegisterRepository = new UserRegisterRepository(ContextDb) );
            } 
        }

        private CertRegisterRepository _certRegisterRepository;
        public CertRegisterRepository CertRegisterRepository
        {
            get
            {
                return _certRegisterRepository ?? ( _certRegisterRepository = new CertRegisterRepository( ContextDb ) );
            }
        }

        private UserSkziRepository _userSkziRepository;
        public UserSkziRepository UserSkziRepository
        {
            get
            {
                return _userSkziRepository ?? ( _userSkziRepository = new UserSkziRepository( ContextDb ) );
            }
        }

        private DepartmentRepository _departmentRepository;
        public DepartmentRepository DepartmentRepository
        {
            get { return _departmentRepository ?? ( _departmentRepository = new DepartmentRepository( ContextDb ) ); }
        }

        private DepartmentAddressRepository _departmentAddressRepository;
        public DepartmentAddressRepository DepartmentAddressRepository
        {
            get { return _departmentAddressRepository ?? ( _departmentAddressRepository = new DepartmentAddressRepository( ContextDb ) ); }
        }

        private RequestRepository _requestRepository;
        public RequestRepository RequestRepository
        {
            get { return _requestRepository ?? ( _requestRepository = new RequestRepository( ContextDb ) ); }
        }

        private WorkKindParamRepository _workKindParamRepository;
        public WorkKindParamRepository WorkKindParamRepository
        {
            get { return _workKindParamRepository ?? ( _workKindParamRepository = new WorkKindParamRepository( ContextDb ) ); }
        }

        private RequestWorkKindParamRepository _requestWorkKindParamRepository;
        public RequestWorkKindParamRepository RequestWorkKindParamRepository
        {
            get { return _requestWorkKindParamRepository ?? ( _requestWorkKindParamRepository = new RequestWorkKindParamRepository( ContextDb ) ); }    
        }

        private RequestWorkKindRepository _requestWorkKindRepository;
        public RequestWorkKindRepository RequestWorkKindRepository
        {
            get { return _requestWorkKindRepository ?? ( _requestWorkKindRepository = new RequestWorkKindRepository( ContextDb ) ); }    
        }

        private WorkKindRepository _workKindRepository;
        public WorkKindRepository WorkKindRepository
        {
            get
            {
                return _workKindRepository ?? ( _workKindRepository = new WorkKindRepository( ContextDb ) );
            }
        }

        private UserPCRepository _userPCRepository;
        public UserPCRepository UserPCRepository
        {
            get
            {
                return _userPCRepository ?? ( _userPCRepository = new UserPCRepository( ContextDb ) );
            }
        }

        private SkziKindRepository _skziKindRepository;
        public SkziKindRepository SkziKindRepository
        {
            get
            {
                return _skziKindRepository ?? ( _skziKindRepository = new SkziKindRepository( ContextDb ) );
            }
        }

        private SystemUserRepository _systemUserRepository;
        public SystemUserRepository SystemUserRepository
        {
            get
            {
                return _systemUserRepository ?? ( _systemUserRepository = new SystemUserRepository( ContextDb ) );
            }
        }

        private PCRepository _pcRepository;
        public PCRepository PCRepository
        {
            get
            {
                return _pcRepository ?? ( _pcRepository = new PCRepository( ContextDb ) );
            }
        }

        private UserHistoryRepository _userHistoryRepository;
        public UserHistoryRepository UserHistoryRepository
        {
            get { return _userHistoryRepository ?? ( _userHistoryRepository = new UserHistoryRepository(ContextDb) ); }
        }

        private PCStickerRepository _pcStickerRepository;
        public PCStickerRepository PCStickerRepository
        {
            get { return _pcStickerRepository ?? ( _pcStickerRepository = new PCStickerRepository( ContextDb ) ); }
        }

        #endregion

        public void Dispose()
        {
            if ( ContextDb != null )
            {
                ContextDb.Dispose();
                ContextDb = null;
            }
            GC.SuppressFinalize( this );
        }
    }
}
